package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.Contents

import hlhj.fhp.supreme.MainActivity
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.RegisBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import hlhj.fhp.supreme.utils.SPHelper
import java.util.*

class InitAty : BaseAty() {
    private lateinit var timer:Timer
    override fun getContentId(): Int {
        return  R.layout.activity_init_aty
    }

    override fun initView() {
        timer= Timer()

        val task=object :TimerTask(){
            override fun run() {
                if (SPHelper.getIsFirst(this@InitAty)){
                    startActivity(Intent(this@InitAty,NavAty::class.java))
                    finish()
                }else{
                    isLogined()
                }
            }
        }
        timer.schedule(task,3000)
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }

    private fun isLogined() {
        if (SPHelper.getLoginStatus(this)){
            OkGo.post<RegisBean>(Urls.BaseUrl+ Urls.Login)
                    .params("mobile",SPHelper.getUserName(this))
                    .params("user_pass",SPHelper.getPwd(this))
                    .execute(object : JsonCallBack<RegisBean>(this){
                        override fun onSuccess(response: Response<RegisBean>?) {
//                            loadingDialog.dismiss()
                            val bean = response?.body()
                            if (bean?.code==1){
                                Contents.TOKEN=bean?.data.token
                                SPHelper.saveToken(this@InitAty,bean.data.token)
                                val intent = Intent(this@InitAty, MainActivity::class.java)
                                startActivity(intent)
                                finish()
                            }else{
                                MyUtils.toast(bean?.msg)
                            }
                        }
                    })
        }else{
            startActivity(Intent(this@InitAty,LoginAty::class.java))
        }
    }
}
